package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.repo.IAlbumRedisRepository;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AlbumServiceImpl implements IAlbumService {
    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private IAlbumRedisRepository albumRedisRepository;
    @Override
    public void addNew(AlbumAddNewDTO albumAddNewDTO) {
        log.debug("开始处理【添加相册】的业务,参数: {}",albumAddNewDTO);
        // 调用参数对象的getName()得到尝试添加的相册的名称
        String name = albumAddNewDTO.getName();
        // 调用Mapper对象的countByName()执行统计查询
        int count = albumMapper.countByName(name);
        log.debug("根据名称统计数量:{}",count);
        // 判断统计结果是否大于0
        if (count > 0){
            // -- 是：名称被占用，抛出异常，例如：throw new RuntimeException()
            String message = "相册名称被占用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        // 创建Album对象
        Album album = new Album();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到Album对象中
        BeanUtils.copyProperties(albumAddNewDTO,album);
        // 调用Mapper对象的insert()执行插入相册数据
        log.debug("插入数据:{}",album);
        int rows = albumMapper.insert(album);
        if (rows != 1){
            String message = "添加相册失败,服务器忙,请稍后再尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
    }

    @Override
    public void delete(Long id){
        log.debug("开始处理【根据ID删除相册】的业务,参数: {}",id);
        AlbumStandardVO album = albumMapper.getStandardById(id);
        log.debug("根据ID={}检查相册数据是否存在,查询结果: {}",id,album);
        if (album == null){
            String message = "尝试删除的相册数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        log.debug("即将执行删除,参数: {}",id);
        int rows = albumMapper.deleteById(id);
        if (rows != 1){
            String message = "删除相册失败,服务器忙,请稍后再尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
    }

    @Override
    public void updateInfo(Long id, AlbumUpdateDTO albumUpdateDTO) {
        log.debug("开始处理【修改相册】的业务,ID: {}, 新数据: {}",id,albumUpdateDTO);
        AlbumStandardVO queryResult = albumMapper.getStandardById(id);
        if (queryResult == null){
            String message = "尝试修改的相册数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        int count = albumMapper.countByNameAndNotId(id, albumUpdateDTO.getName());
        if (count > 0){
            String message = "修改失败!相册名称被占用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        Album album = new Album();
        BeanUtils.copyProperties(albumUpdateDTO,album);
        album.setId(id);
        int rows = albumMapper.update(album);
        if (rows != 1){
            String message = "修改相册失败,服务器忙,请稍后再尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public AlbumStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询相册详情】的业务,参数: {}",id);
        //AlbumStandardVO queryResult = albumMapper.getStandardById(id);
        AlbumStandardVO queryResult = albumRedisRepository.get(id);
        if (queryResult == null){
            String message = "查询失败!相册数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return queryResult;
    }

    @Override
    public List<AlbumListItemVO> list() {
        log.debug("开始处理【相册列表】的业务,无参数");
        List<AlbumListItemVO> list = albumRedisRepository.list();
        return list;
    }

    @Override
    public void rebuildCache() {
        log.debug("开始处理【重建相册缓存】的业务, 无参数");
        albumRedisRepository.deleteList();
        List<AlbumListItemVO> list = albumMapper.list();
        albumRedisRepository.save(list);

        albumRedisRepository.deleteAllItem();
        for (AlbumListItemVO albumListItemVO : list){
            AlbumStandardVO albumStandardVO = albumMapper.getStandardById(albumListItemVO.getId());
            albumRedisRepository.save(albumStandardVO);
        }
    }
}
